package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    public static int[] numberArray = new int[4];
    public static int[] guessArray = new int[4];
    public static int a_num = 0;
    public static int b_num = 0;

    public static void main(String[] args) {
        System.out.println("=====新猜数字游戏=====");
        while(true) {
            System.out.println("=====输入1开始游戏=====");
            System.out.println("=====输入0结束游戏=====");
            Scanner input = new Scanner(System.in);
            switch (input.nextInt()) {
                case 0:
                    System.out.println("=====已退出游戏=====");
                    System.exit(0);
                    break;
                case 1:
                    int num_1 = 0;
                    int num_2 = 7;
                    System.out.println("=====已开始游戏(限制次数为7次)=====");
                    generateAnswer();
                    while (true) {
                        getPlayerInput();
                        compareGuessAnswer();
                        if (isWin() == true) {
                            System.out.println("赢了");
                            break;
                        } else {
                            num_1++;
                            num_2--;
                            if(num_1==7){
                                System.out.println("7次到了，正确答案为"+Arrays.toString(numberArray));
                                System.out.println("=====再来一局=====");
                                break;
                            }
                            System.out.println("猜错了，在来一次！(还剩"+num_2+"次)");
                        }
                    }
                    break;
            }
        }
    }

    public  static int[] generateAnswer(){
        int index = 0;
        while(true){
            int random = (int)(Math.random() * 9 + 1);
            int i = 0;
            for ( ; i < numberArray.length; i++) {
                if(random == numberArray[i]) {
                    break;
                }
            }
            if(i == numberArray.length ) {
                numberArray[index++] = random;
            }
            if (index == 4) {
                break;
            }
        }
        return numberArray;
    }
    public static int[] getPlayerInput(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入0~9的4个数");
        for (int i= 0; i < guessArray.length; i++) {
            guessArray[i] = scanner.nextInt();
            if (guessArray[i] >= 10 || guessArray[i] < 0) {
                System.out.println("输入错误");
                break;
            }
        }
        System.out.println("猜的4个数为:" + Arrays.toString(guessArray));
        return guessArray;
    }

    public static String compareGuessAnswer(){
        a_num = 0;
        b_num = 0;
        for (int i = 0; i < numberArray.length; i++) {
            for (int j = 0; j < guessArray.length; j++) {
                if (i == j) {
                    if (numberArray[i]==guessArray[j]){
                        a_num++;
                    }
                }else {
                    if (numberArray[i]==guessArray[j]){
                        b_num++;
                    }
                }
            }
        }
        String tiShi = a_num+"A"+b_num+"B\nA是位置和数字都猜对了，B是数字对位置错";
        System.out.println(tiShi);
        return tiShi;
    }

    public static boolean isWin(){
        boolean flag = false;
        if (a_num==4){
            flag=true;
        }else {
            flag=false;
        }
        return flag;
    }





}
